package com.app.piemoment.entity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class QASubject {
	private String code;
	private String title;
	private String type;
	private String answer;
	private String[] answers;

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getAnswer() {
		return answer;
	}

	public void setAnswer(String answer) {
		this.answer = answer;
	}

	public String[] getAnswers() {
		return answers;
	}

	public void setAnswers(String[] answers) {
		this.answers = answers;
	}

	public void parser(JSONObject obj) throws JSONException {
		if (obj != null) {
			if(obj.has("answer")) {
				setAnswer(obj.getString("answer"));
			}
			if(obj.has("question")) {
				JSONObject jsonObject = obj.getJSONObject("question");
				if (jsonObject != null) {
					if(jsonObject.has("code")) {
						setCode(jsonObject.getString("code"));
					}
					if(jsonObject.has("type")) {
						setType(jsonObject.getString("type"));
					}
					if(jsonObject.has("title")) {
						setTitle(jsonObject.getString("title"));
					}
					if(jsonObject.has("possible_answers")) {
						JSONArray array = jsonObject.getJSONArray("possible_answers");
						if(array!=null&&array.length()>0) {
							answers = new String[array.length()];
							for(int i=0;i<array.length();i++) {
								answers[i] = (String) array.opt(i);
							}
							setAnswers(answers);
						}
					}
				}	
			}
			
		}

	}

}
